Visualforce পেজে SOQL এবং DML ব্যবহার হলো Apex ক্লাসের মাধ্যমে Salesforce ডেটাবেস থেকে ডেটা পুনরুদ্ধার এবং পরিবর্তন করার পদ্ধতি। Visualforce পেজে SOQL এবং DML ব্যবহার করা হয় Apex ক্লাসের মধ্যে, যা ডেটা ফিল্টারিং, ফিল্ড আপডেট, নতুন রেকর্ড তৈরি, অথবা রেকর্ড মুছে ফেলার জন্য সহায়ক। তবে এটি করতে গিয়ে গভর্নর লিমিট মেনে চলা গুরুত্বপূর্ণ, যাতে কোডের কার্যকারিতা ও স্থায়িত্ব বজায় থাকে।
Visualforce পেজে SOQL এবং DML সরাসরি ব্যবহার করা হয় না; বরং একটি Custom Controller বা Controller Extension-এর মাধ্যমে SOQL এবং DML অপারেশন করা হয়। SOQL ব্যবহার করে ডেটাবেস থেকে ডেটা ফিল্টার করা হয়, এবং DML অপারেশন ব্যবহার করে সেই ডেটায় পরিবর্তন আনা হয়।
SOQL ব্যবহার করে ডেটাবেস থেকে ডেটা পুনরুদ্ধার করা যায় এবং Visualforce পেজে তা প্রদর্শন করা হয়। SOQL ব্যবহার করে সাধারণত কাস্টম লজিকের মাধ্যমে ডেটা ফিল্টার করা হয়।
Apex Class (Custom Controller)
public class AccountController {
public List<Account> accountList { get; set; }
public AccountController() {
accountList = [SELECT Id, Name, Industry FROM Account LIMIT 10];
}
}
Visualforce Page
<apex:page controller="AccountController">
<h1>Account List</h1>
<apex:pageBlock title="Accounts">
<apex:pageBlockTable value="{!accountList}" var="acc">
<apex:column value="{!acc.Name}" headerValue="Account Name"/>
<apex:column value="{!acc.Industry}" headerValue="Industry"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
accountList
ফিল্ডে SOQL ব্যবহার করে Account রেকর্ডগুলো পুনরুদ্ধার করা হয়েছে।apex:pageBlockTable
ব্যবহার করা হয়েছে।DML অপারেশন ব্যবহার করে Visualforce পেজে ডেটা আপডেট, নতুন রেকর্ড তৈরি বা মুছে ফেলা যায়। সাধারণত commandButton
বা commandLink
এর মাধ্যমে DML অপারেশন ট্রিগার করা হয়।
Apex Class (Custom Controller)
public class AccountController {
public Account acc { get; set; }
public AccountController() {
acc = new Account();
}
public void saveAccount() {
insert acc;
}
}
Visualforce Page
<apex:page controller="AccountController">
<h1>Create New Account</h1>
<apex:form>
<apex:inputField value="{!acc.Name}" label="Account Name"/>
<apex:inputField value="{!acc.Industry}" label="Industry"/>
<apex:commandButton value="Save Account" action="{!saveAccount}" rerender="confirmationMessage"/>
<apex:outputPanel id="confirmationMessage">
<apex:outputText value="Account has been saved successfully!" rendered="{!NOT(ISNULL(acc.Id))}"/>
</apex:outputPanel>
</apex:form>
</apex:page>
saveAccount
মেথড ব্যবহার করে DML insert
অপারেশন করা হয়েছে, যা নতুন Account রেকর্ড তৈরি করে।commandButton
ব্যবহার করে saveAccount
মেথড কল করা হয়েছে, যা রেকর্ড সেভ করবে এবং সফল হলে একটি কনফার্মেশন মেসেজ প্রদর্শন করবে।LIMIT
ব্যবহার করুন।public class AccountController {
public List<Account> accountList { get; set; }
public Account acc { get; set; }
public AccountController() {
accountList = [SELECT Id, Name, Industry FROM Account LIMIT 10];
acc = new Account();
}
public void saveAccount() {
insert acc;
}
}
<apex:page controller="AccountController">
<h1>Account List and Creation</h1>
<apex:pageBlock title="Create New Account">
<apex:form>
<apex:inputField value="{!acc.Name}" label="Account Name"/>
<apex:inputField value="{!acc.Industry}" label="Industry"/>
<apex:commandButton value="Save Account" action="{!saveAccount}" rerender="confirmation"/>
<apex:outputPanel id="confirmation">
<apex:outputText value="Account saved!" rendered="{!NOT(ISNULL(acc.Id))}"/>
</apex:outputPanel>
</apex:form>
</apex:pageBlock>
<apex:pageBlock title="Account List">
<apex:pageBlockTable value="{!accountList}" var="acc">
<apex:column value="{!acc.Name}" headerValue="Account Name"/>
<apex:column value="{!acc.Industry}" headerValue="Industry"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
@isTest
public class AccountControllerTest {
@isTest
static void testSOQLQuery() {
Account acc1 = new Account(Name = 'Test Account 1', Industry = 'Technology');
insert acc1;
AccountController controller = new AccountController();
System.assertNotEquals(0, controller.accountList.size(), 'Account list should not be empty');
}
@isTest
static void testDMLInsert() {
AccountController controller = new AccountController();
controller.acc.Name = 'Test Account 2';
controller.acc.Industry = 'Healthcare';
Test.startTest();
controller.saveAccount();
Test.stopTest();
Account insertedAcc = [SELECT Id, Name, Industry FROM Account WHERE Id = :controller.acc.Id];
System.assertEquals('Test Account 2', insertedAcc.Name, 'Account name should match');
System.assertEquals('Healthcare', insertedAcc.Industry, 'Industry should match');
}
}
accountList
এর জন্য টেস্ট ক্লাস তৈরি করা হয়েছে।insert
অপারেশন টেস্ট করার জন্য টেস্ট মেথড তৈরি করা হয়েছে, যা নতুন Account রেকর্ড তৈরি এবং ভ্যালিডেশন করে।Visualforce পেজে SOQL এবং DML ব্যবহার করে ডেটাবেস থেকে ডেটা পুনরুদ্ধার ও পরিবর্তন করার মাধ্যমে Apex কোডের কার্যক্ষমতা এবং ডেটা প্রক্রিয়াকরণ সক্ষমতা বৃদ্ধি করা যায়। Custom Controller এবং Controller Extension ব্যবহার করে SOQL ও DML অপারেশন প্রয়োগ করতে হলে গভর্নর লিমিট মেনে চলা, টেস্ট ক্লাস ও টেস্ট ডেটা তৈরি করা এবং সঠিক Exception Handling নিশ্চিত করা গুরুত্বপূর্ণ।
common.read_more